package com.bilibili.server.controller;

import com.bilibili.model.vo.PageResult;
import com.bilibili.model.vo.SettingsVo;
import com.bilibili.model.vo.UserInfoVoAge;
import com.bilibili.server.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author HuangRongFu
 * 2021/11/17 星期三9:15 下午
 */

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;


    /**
     * 用户通用设置 - 读取
     *
     * @Return: ResponseEntity
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {

        SettingsVo vo = settingsService.querySettings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题 保存
     *
     * @params: content
     * @Return: ResponseEntity
     */
    @PostMapping("/questions")
    public ResponseEntity saveQuestions(@RequestBody Map<String, String> paramMap) {

        settingsService.saveQuestions(paramMap);
        return ResponseEntity.ok(null);
    }

    /**
     * 通用设置保存
     *
     * @param: vo
     * @Return: ResponseEntity
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity saveSettings(@RequestBody SettingsVo vo) {

        settingsService.saveSettings(vo);
        return ResponseEntity.ok(null);
    }

    /**
     * 黑名单列表
     *
     * @param: page
     * @param: pageSize
     * @Return: ResponseEntity
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(value = "page", defaultValue = "1") Long page,
                                    @RequestParam(value = "pagesize", defaultValue = "10") Long pageSize) {

        //要构建页面返回值
        //需要限制页面的大小  如果页面大小大于50 设置最大值50  ＜50用原值  利用三元运算
        pageSize = pageSize > 50 ? 50 : pageSize;

        PageResult<UserInfoVoAge> PageResult = settingsService.blackList(page, pageSize);
        return ResponseEntity.ok(PageResult);
    }

    /**
     * 移除黑名单
     *@param: blackUserId
     *@Return: ResponseEntity
     */
    @DeleteMapping("/blacklist/{blackUserId}")
    public ResponseEntity removeBlackList(@PathVariable Long blackUserId){

        settingsService.removeBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
}
